package controller;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.util.JsonResult;

import entity.SysRole;
import entity.SysUser;
import service.SysRoleService;
import service.SysUserService;
/**
 * Created by wenchao.ren on 2014/4/26.
 */
@SpringBootApplication
//启注解事务管理  
@Controller
@Import ({SysUserService.class,SysRoleService.class})
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleService sysRoleService;

	@RequestMapping(value = "sysUserList", method = RequestMethod.GET)
    public String sysUserList(Model model) {
    	//ModelAndView view = new ModelAndView("/sys/sysUserList");
    	List<SysUser> sysUserList = sysUserService.find();
    	System.out.println("sysUserList.size()==========="+sysUserList.size());
    	//view.addObject("sysUserList", sysUserList);
    	model.addAttribute("sysUserList", sysUserList);
        return "sys/sysUserList";
    }
	
	
	@ResponseBody
	@RequestMapping(value = "sysUser_list.json")
	public List<SysUser> listSysUser() {
		List<SysUser> userList = sysUserService.find();
		return userList;
	}
	
	@RequestMapping(value = "sysUserEdit", method = RequestMethod.GET)
    public String userEdit(Long  seqid,Model model) {
    	SysUser sysUser = new SysUser();
    	if(seqid!=null){
    		sysUser = sysUserService.findById(seqid);
    	}
    	List<SysRole> sysRoleList = sysRoleService.find();
    	
    	model.addAttribute("sysUser", sysUser);
    	model.addAttribute("sysRoleList", sysRoleList);
    	 return "sys/sysUserEdit";
    }
	
	/**
	 * 保存用户修改信息
	 * 
	 * @param tblStage
	 * @return
	 */
	@RequestMapping(value = "sysUser_save.json")
	@ResponseBody
	public JsonResult saveUser(SysUser sysUser) {
		JsonResult result = JsonResult.newInstance();	
		sysUserService.save(sysUser);
		return result;
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "sysUser_del.json")
	@ResponseBody
	public JsonResult delUsers(String ids) {
		JsonResult result = JsonResult.newInstance();
		sysUserService.deleteByIds(ids.split(","));
		return result;
	}
	
	
    
}
